Advent of Code 2020: Day 5
Part of the series Advent of Code 2020 (5 posts total).- Aug 26, 2022 - Advent of Code 2020: Intro
- Aug 29, 2022 - Advent of Code 2020: Day 5
- Aug 31, 2022 - Advent of Code 2020: Day 7
- Sep 01, 2022 - Advent of Code 2020: Day 9
- Sep 04, 2022 - Advent of Code 2020: Day 13
Advent of Code Day 5 wasn’t too difficult but it was a fun exercise. You’re given a bunch of airplane tickets, where the row and seat number is given as a string of 10 characters. The airplane has a total of
An F indicates that the seating is in the first half of possibilities and a B indicates that the seating is in the second half of possibilities. By halving the amount of rows
Row 0-127 Seat 0-7
[F] B F B B F F R L R Row 0-63 Seat 0-7
F [B] F B B F F R L R Row 32-63 Seat 0-7
F B [F] B B F F R L R Row 32-47 Seat 0-7
F B F [B] B F F R L R Row 40-47 Seat 0-7
F B F B [B] F F R L R Row 44-47 Seat 0-7
F B F B B [F] F R L R Row 44-45 Seat 0-7
F B F B B F [F] R L R Row 44 Seat 0-7
F B F B B F F [R] L R Row 44 Seat 4-7
F B F B B F F R [L] R Row 44 Seat 4-5
F B F B B F F R L [R] Row 44 Seat 5
Part 1
In part 1 of the challenge we simply have to find the highest ID of the tickets provided. This ID is given as
|
|
A second function creates a list of rows and of columns and repeatedly calls this function, that is, 7 times for the row listing and 3 times for the column.
|
|
This produces an array of size
|
|
The answer to part 1 is the maximum seat ID, which is easily found by max()
.
Part 2
The description for part two on the website is sufficient to explain this part:
It’s a completely full flight, so your seat should be the only missing boarding pass in your list. However, there’s a catch: some of the seats at the very front and back of the plane don’t exist on this aircraft, so they’ll be missing from your list as well. Your seat wasn’t at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list.
I could just iterate over it and find the missing one, but keeping in line with the numpy broadcasting, I decided on a super speed solution. I’ll explain my code by example: Assume ID’s
Seating ID's Array Difference
3 3 0
4 4 0
5 5 0
7 6 1
8 7 1
The moment a seat is missing the difference becomes 1. The numpy function argmax()
finds the index at which this first occurs, and the answer becomes the seat ID at this index minus 1.
The full code for this day can be found here. The entire repository is available at GitHub.
Published 29. August 2022
Last modified 29. August 2022